package com.wantong.jianpu.viewmodels;

import android.app.Application;

import androidx.annotation.NonNull;

import com.wantong.jianpu.base.BaseResultBean;
import com.wantong.jianpu.event.FaultDetailEvent;
import com.wantong.jianpu.models.FaultModel;
import com.wantong.jianpu.net.BaseObserver;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;
import me.goldze.mvvmhabit.utils.StringUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;

/**
 * @author PACY
 * @date 2022/4/29
 * description:
 */
public class FaultHandleViewModel extends BaseViewModel {
    public String id;

    public FaultModel model;

    public SingleLiveEvent<Boolean> showRemark=new SingleLiveEvent<>();
    public SingleLiveEvent<String> remark=new SingleLiveEvent<>();
    public SingleLiveEvent<String> state=new SingleLiveEvent<>();//故障上报
    public SingleLiveEvent<String[]> stateDialogEvent=new SingleLiveEvent<>();

    public FaultHandleViewModel(@NonNull Application application) {
        super(application);
        model=new FaultModel();
    }

    @Override
    protected void init() {
        super.init();
    }

    /**
     * 提交
     */
    public BindingCommand commit=new BindingCommand(() -> {
        if(StringUtils.isEmpty(state.getValue())){
            ToastUtils.show("请选择处理方式");
            return;
        }

        if(showRemark.getValue()){
            if(StringUtils.isEmpty(remark.getValue())){
                ToastUtils.show("请输入退回原因");
                return;
            }else{
                gdth();
            }
        }else{
            commitRequest();
        }
    });


    /**
     * 维修状态
     */
    public BindingCommand selectState=new BindingCommand(() -> {
        stateDialogEvent.setValue(new String[]{"上报维修", "退回"});
    });

    /**
     * 设置维修状态
     * @param str
     */
    public void setState(String str){
        state.setValue(str);
        remark.setValue("");
        if(str.equals("上报维修")){
            showRemark.setValue(false);
        }else {
            showRemark.setValue(true);
        }
    }


    /**
     * 确认上报请求
     */
    public void commitRequest(){
        Map<String,Object> map=new HashMap<>();
        map.put("repairOrderId",id);
        map.put("remark",remark.getValue());
        showDialog();
        model.order_submit(map, new BaseObserver() {
            @Override
            public void onSuccess(BaseResultBean t) {
                ToastUtils.show("提交成功");
                EventBus.getDefault().post(new FaultDetailEvent());
                finish();
            }

            @Override
            public void onComplete() {
                super.onComplete();
                dismissDialog();
            }
        },getLifecycleProvider());
    }


    /**
     * 工单退回
     */
    public void gdth(){
        Map<String,Object> map=new HashMap<>();
        map.put("repairOrderId",id);
        map.put("remark",remark.getValue());
        showDialog();
        model.gdth(map, new BaseObserver() {
            @Override
            public void onSuccess(BaseResultBean t) {
                ToastUtils.show("退回成功");
                EventBus.getDefault().post(new FaultDetailEvent());
                finish();
            }

            @Override
            public void onComplete() {
                super.onComplete();
                dismissDialog();
            }
        },getLifecycleProvider());
    }
}
